/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.product.service.impl;

import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.enums.BillDetailEnum;
import co.yixiang.enums.BillInfoEnum;
import co.yixiang.modules.product.domain.YxPrizeRecord;
import co.yixiang.modules.product.service.YxPrizeRecordService;
import co.yixiang.modules.product.service.dto.YxPrizeRecordDto;
import co.yixiang.modules.product.service.dto.YxPrizeRecordQueryCriteria;
import co.yixiang.modules.product.service.dto.YxPrizeRecordQueryVo;
import co.yixiang.modules.product.service.mapper.YxPrizeRecordMapper;
import co.yixiang.modules.user.domain.YxUserBill;
import co.yixiang.modules.user.vo.BillVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import co.yixiang.domain.PageResult;
/**
* @author lsh
* @date 2021-10-21
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxPrizeRecord")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxPrizeRecordServiceImpl extends BaseServiceImpl<YxPrizeRecordMapper, YxPrizeRecord> implements YxPrizeRecordService {

    private final IGenerator generator;
    private final YxPrizeRecordMapper yxPrizeRecordMapper;

    @Override
    //@Cacheable
    public PageResult<YxPrizeRecordDto> queryAll(YxPrizeRecordQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxPrizeRecord> page = new PageInfo<YxPrizeRecord>(queryAll(criteria));
        return generator.convertPageInfo(page,YxPrizeRecordDto.class);
    }


    @Override
    //@Cacheable
    public List<YxPrizeRecord> queryAll(YxPrizeRecordQueryCriteria criteria){
        //return yxPrizeRecordMapper.queryAll(criteria.getUid(),criteria.getNickname(),criteria.getPhone());
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxPrizeRecord.class, criteria));
    }

    /**
     * 获取抽奖记录
     * @param page page
     * @param limit limit
     * @param uid uid
     * @param type BillDetailEnum
     * @return list
     */
    @Override
    public List<YxPrizeRecord> getPrizeRecordList(long uid, int type,int page, int limit) {

        Page<YxPrizeRecord> pageModel = new Page<>(page, limit);
        QueryWrapper<YxPrizeRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(YxPrizeRecord::getUid,uid).eq(YxPrizeRecord::getPrizeStatus,type).orderByDesc(YxPrizeRecord::getId);
        List<YxPrizeRecord> prizeRecordList = yxPrizeRecordMapper.getPrizeRecordList(wrapper,pageModel);

        return prizeRecordList;
    }


    @Override
    public void download(List<YxPrizeRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxPrizeRecordDto yxPrizeRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户uid", yxPrizeRecord.getUid());
            map.put("奖品名称", yxPrizeRecord.getTitle());
            map.put("微信昵称", yxPrizeRecord.getNickname());
            map.put("收货人", yxPrizeRecord.getRealName());
            map.put("收货人电话", yxPrizeRecord.getUserPhone());
            map.put("详细地址", yxPrizeRecord.getUserAddress());
            map.put("抽奖时间", yxPrizeRecord.getCreateTime());
            map.put("竞奖时间", yxPrizeRecord.getPrizeTime());
            map.put("备注", yxPrizeRecord.getMark());
            /*map.put(" updateTime",  yxPrizeRecord.getUpdateTime());
            map.put("0 = 带确定 1 = 有效 -1 = 无效", yxPrizeRecord.getStatus());*/
            map.put(" isDel",  yxPrizeRecord.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
